﻿using DotNetExtensions.AspNet;
using DotNetExtensions.AspNet.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace AspNetSimple.IOC
{
    class IOC_Simple
    {
        public IOC_Simple()
        {
            ContainerBuilder builder = ContainerBuilder.Default;

            builder.RegistTypes(option =>
            {
                option.Assemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies());
                option.Filters.Add(new InjectTypeFilter
                {
                    Kind = InjectKind.Named,
                    Type = typeof(IProvider)
                });
            });

            
            Timer timer = new Timer(new TimerCallback(obj =>
            {
                // 如果不指定名称 则使用类全名称 = typeof(Provider1).FullName
                var provider2 = builder.ResolveNamed<IProvider>("Provider1");
                provider2.Write();

                var provider1 = builder.ResolveNamed<IProvider>("Provider2");
                provider1.Write();

                object fullname1 = builder.Resolve("AspNetSimple.IOC.FullNmaeTest");
                object fullname2 = builder.Resolve("AspNetSimple.IOC.FullNmaeTestSingle");

                (fullname1 as FullNmaeTest).Hello();
                (fullname2 as FullNmaeTestSingle).Hello();
            }),null, 1000, 1000);
        }
    }

    public interface IProvider
    {
        void Write();
    }

    [Inject(Scope = InjectScope.Single, Named = "Provider1")]
    public class Provider1 : IProvider
    {
        private string text;
        public Provider1()
        {
            text = DateTime.Now.ToString();
        }
        public void Write()
        {
            Console.WriteLine(text);
        }
    }
    [Inject(Scope = InjectScope.Instance, Named = "Provider2")]
    public class Provider2 : IProvider
    {
        private string text;
        public Provider2()
        {
            text = Snowflake.Default.NextId().ToString();
        }
        public void Write()
        {
            Console.WriteLine(text);
        }
    }


    [AutoInject]
    public class FullNmaeTest
    {
        public FullNmaeTest()
        {

        }

        public void Hello()
        {
            Console.WriteLine("Hello im FullNmaeTest");
        }
    }
    [Inject(Scope = InjectScope.Single)]
    [AutoInject]
    public class FullNmaeTestSingle
    {
        private readonly DateTime now;
        public FullNmaeTestSingle()
        {
            now = DateTime.Now;
        }

        public void Hello()
        {
            Console.WriteLine($"{now}:Hello im FullNmaeTestSingle");
        }
    }
}
